/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.admin.client.components;

import java.util.ArrayList;
import java.util.List;

import com.elf.gwt.admin.client.AdminComponentController;
import com.elf.gwt.common.client.model.OrderStatusModel;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.TextBox;

/**
 *  
 */
public class OrderStatusComponent extends EditTableComponent
{	
	protected OrderStatusModel emptyOrderStatusModel;
	protected OrderStatusModel orderStatusModel;
	protected List<OrderStatusModel> orderStatusModels;

	/**
	 * Constructor
	 */
	public OrderStatusComponent(AdminComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix);
		paginate = false;
		emptyOrderStatusModel = new OrderStatusModel();
		orderStatusModels = new ArrayList<OrderStatusModel>();
	}

	protected void buildMetaModel(JSONObject jsonObject) throws Exception
	{
	}

	protected void buildModel(JSONArray jsonArray) throws Exception
	{
		orderStatusModels.clear();
		super.buildModel(jsonArray);
	}

	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		OrderStatusModel model = new OrderStatusModel(jsonObject);
		orderStatusModels.add(model);
	}

	public void buildFromModel() throws Exception
	{
		numberOfTableEntries = orderStatusModels.size();
		if (edit)
			numberOfTableColumns = 5;
		else
			numberOfTableColumns = 4;
		extraRows = 2;
		super.buildFromModel();
	}

	public void displayHeader(Grid grid) {
		grid.setText(0, 0, "Id");
		grid.setText(0, 1, "Name");
		grid.setText(0, 2, "Public Flag");
		grid.setText(0, 3, "Downloads Flag");
		if (edit) grid.setText(0, 4, "Mark for Deletion");
	}

	public void displayRow(Grid grid, int row, int entryIndex) {
		displayRow(grid, row, orderStatusModels.get(entryIndex));
	}

	public void displayEmptyRow(Grid grid, int row) {
		displayRow(grid, row, emptyOrderStatusModel);
	}

	public void displayRow(Grid grid, int row, OrderStatusModel model) {
		if (edit) {
			TextBox idTextBox = new TextBox();
			idTextBox.setWidth("80px");
			idTextBox.setText(String.valueOf(model.getId()));
			elements.put("orderStatusId-" + row, idTextBox);
			grid.setWidget(row, 0, idTextBox);

			TextBox nameTextBox = new TextBox();
			nameTextBox.setWidth("160px");
			nameTextBox.setText(model.getName());
			elements.put("orderStatusName-" + row, nameTextBox);
			grid.setWidget(row, 1, nameTextBox);

			CheckBox publicFlagCheckBox = new CheckBox();
			publicFlagCheckBox.setValue(model.isPublicFlag());
			elements.put("orderStatusPublicFlag-" + row, publicFlagCheckBox);
			grid.setWidget(row, 2, publicFlagCheckBox);

			CheckBox downloadsFlagCheckBox = new CheckBox();
			downloadsFlagCheckBox.setValue(model.isDownloadsFlag());
			elements.put("orderStatusDownloadsFlag-" + row, downloadsFlagCheckBox);
			grid.setWidget(row, 3, downloadsFlagCheckBox);

			if (model.getId() > -1) {
				CheckBox deleteCheckBox = new CheckBox();
				elements.put("orderStatusDelete-" + row, deleteCheckBox);
				grid.setWidget(row, 4, deleteCheckBox);
			}
		}
		else {
			grid.setText(row, 0, String.valueOf(model.getId()));
			grid.setText(row, 1, model.getName());
			grid.setText(row, 2, String.valueOf(model.isPublicFlag()));
			grid.setText(row, 3, String.valueOf(model.isDownloadsFlag()));
		}
	}

	public List<OrderStatusModel> getOrderStatusModels() {
		return orderStatusModels;
	}
}
